package com.caowang.mhtlj.controller;

import com.caowang.mhtlj.model.LogAndPubCard;
import com.caowang.mhtlj.model.PrivateCard;
import com.caowang.mhtlj.model.PublicCard;
import com.caowang.mhtlj.model.User;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @PackageName:com.caowang.mhtlj.controller
 * @ClassName:MhtljController
 * @Description:
 * @Author:caojia
 * @Date:2022/9/80:22
 */
@RestController
public class MhtljController {
    ConcurrentHashMap<Integer, String> pool = new ConcurrentHashMap<>();
    LinkedList<Map<String, String>> licensingLog = new LinkedList<>();
    LinkedList<Map<String, String>> flopLog = new LinkedList<>();
    String pubCard = new String();
    AtomicBoolean isAdmin = new AtomicBoolean(false);

    Map<String,String> isCheckedMap = new ConcurrentHashMap<>();

    //庄家控制发牌容器
    List<Map<String,String>> zhuangListMap = new ArrayList<>();

    User  admin = new User() ;


    @RequestMapping("/index")
    public String index() {
        return "1";
    }


    @GetMapping("/admin/flash")
    public Result<Integer> flash() {
        pool.put(1, "img/htA.png");
        pool.put(2, "img/ht2.png");
        pool.put(3, "img/ht3.png");
        pool.put(4, "img/ht4.png");
        pool.put(5, "img/ht5.png");
        pool.put(6, "img/ht6.png");
        pool.put(7, "img/ht7.png");
        pool.put(8, "img/ht8.png");
        pool.put(9, "img/ht9.png");
        pool.put(10, "img/ht10.png");
        pool.put(11, "img/htJ.png");
        pool.put(12, "img/htQ.png");
        pool.put(13, "img/htK.png");
        pool.put(14, "img/hongtaoA.png");
        pool.put(15, "img/hongtao2.png");
        pool.put(16, "img/hongtao3.png");
        pool.put(17, "img/hongtao4.png");
        pool.put(18, "img/hongtao5.png");
        pool.put(19, "img/hongtao6.png");
        pool.put(20, "img/hongtao7.png");
        pool.put(21, "img/hongtao8.png");
        pool.put(22, "img/hongtao9.png");
        pool.put(23, "img/hongtao10.png");
        pool.put(24, "img/hongtaoJ.png");
        pool.put(25, "img/hongtaoQ.png");
        pool.put(26, "img/hongtaoK.png");
        pool.put(27, "img/meihuaA.png");
        pool.put(28, "img/meihua2.png");
        pool.put(29, "img/meihua3.png");
        pool.put(30, "img/meihua4.png");
        pool.put(31, "img/meihua5.png");
        pool.put(32, "img/meihua6.png");
        pool.put(33, "img/meihua7.png");
        pool.put(34, "img/meihua8.png");
        pool.put(35, "img/meihua9.png");
        pool.put(36, "img/meihua10.png");
        pool.put(37, "img/meihuaJ.png");
        pool.put(38, "img/meihuaQ.png");
        pool.put(39, "img/meihuaK.png");
        pool.put(40, "img/fangpianA.png");
        pool.put(41, "img/fangpian2.png");
        pool.put(42, "img/fangpian3.png");
        pool.put(43, "img/fangpian4.png");
        pool.put(44, "img/fangpian5.png");
        pool.put(45, "img/fangpian6.png");
        pool.put(46, "img/fangpian7.png");
        pool.put(47, "img/fangpian8.png");
        pool.put(48, "img/fangpian9.png");
        pool.put(49, "img/fangpian10.png");
        pool.put(50, "img/fangpianJ.png");
        pool.put(51, "img/fangpianQ.png");
        pool.put(52, "img/fangpianK.png");
        pool.put(53, "img/guowang.png");
        pool.put(54, "img/wanghou.png");
        pool.put(55, "img/bb.png");
        return Result.success(1);
    }


    Lock lock = new ReentrantLock();

    // 每个人获取自己的牌
    @GetMapping("/getSelfCard")
    private Result<PrivateCard> getSelfCard(@RequestParam("name") String name) {
        if (pool.size() == 0) {
            return Result.fail("亲爱的， 别着急，等庄家先洗牌哦");
        }
        Random random = new Random();
        Integer card = random.nextInt(55) + 1;
        PrivateCard privateCard = new PrivateCard();
        //  池子中存在时发牌
        Boolean b = false;
        try {
            lock.lock();
            b = pool.get(card) != null;
        } finally {
            lock.unlock();
        }
        if (b) {
            privateCard.setSelfCard(pool.get(card));
            Map<String, String> param = new HashMap<>();
            param.put("log", "庄家给" +name+"发了一张牌");
            licensingLog.push(param);
            pool.remove(card);
            privateCard.setTotal(pool.size());
            return Result.success(privateCard);
        } else {
            // 重新获取随机数，发牌
            return getSelfCard(name);
        }
    }

    /**
     * 获取剩余的牌数
     * @return
     */
    @GetMapping("/getTotal")
    private Result<PublicCard> getTotal() {
        PublicCard privateCard = new PublicCard();
        privateCard.setTotal(pool.size());
        return Result.success(privateCard);
    }

    //庄家获取公牌
    @GetMapping("/getPubCard")
    private Result<PublicCard> getPubCard() {

        if (pool.size() == 0) {
            return Result.fail("少球，先洗牌");
        }
        Random random = new Random();
        Integer card = random.nextInt(54) + 1;
        PublicCard publicCard = new PublicCard();
        //  池子中存在时发牌
        Boolean b = false;
        try {
            lock.lock();
            b = pool.get(card) != null;
        } finally {
            lock.unlock();
        }
        if (b) {
            publicCard.setPublicCard(pool.get(card));
            Map<String, String> param = new HashMap<>();
            param.put("log", pool.get(card));
            licensingLog.push(param);
            pubCard = pool.get(card);
            pool.remove(card);
            publicCard.setTotal(pool.size());
            return Result.success(publicCard);
        } else {
            // 重新获取随机数，发牌
            return getPubCard();
        }
    }

    //翻牌
    @GetMapping("/flopMyCard")
    public Result<Integer> flop(@RequestParam("name") String name, @RequestParam("card") String card,@RequestParam(name = "card1",required = false) String card1) {
        Map<String, String> param = new HashMap<>();
        param.put("name", name);
        param.put("log", card);
        flopLog.push(param);
        if(!"".equals(card1) && card1 != null && !"undefined".equals(card1)){
            Map<String, String> param1 = new HashMap<>();
            param1.put("name", name);
            param1.put("log", card1);
            flopLog.push(param1);
        }
        //return "翻牌成功";
        return Result.success(1);
    }

    //获取公牌
    @GetMapping("/getFlopLog")
    public Result<List<Map<String, String>>> getFlopLogCopy() {
        List<Map<String, String>> flopLogCopy = new LinkedList<>();
        if (flopLog.size() <= 10) {
            flopLogCopy = flopLog;
        } else {
            flopLogCopy = flopLog.subList(0, 10);
        }
        LogAndPubCard logAndPubCard = new LogAndPubCard();
        logAndPubCard.setFlopLog(flopLogCopy);
        return Result.successA(flopLogCopy);
    }

    @GetMapping("pubCard")
    public Result<String> getpubCard() {
        String p = "";
        if (pubCard == null || pubCard.equals("")) {
            p = "还没发牌呢，们肝,稍等等哦";
            return Result.fail(p);
        }else{
            p = pubCard;
            return Result.success(p);
        }
    }


    @GetMapping("/getLicensingLog")
    public Result<List<Map<String, String>>> getLicensingLog() {
        List<Map<String, String>> licensingLogCopy = new LinkedList<>();
        if (licensingLog.size() <= 10) {
            licensingLogCopy = licensingLog;
        } else {
            licensingLogCopy = licensingLog.subList(0, 10);
        }
        if (pubCard == null || pubCard.equals("") && licensingLogCopy.isEmpty()) {
            return Result.fail("牌还没发呢，急球呢");
        }
//        logAndPubCard.setLicensingLog(licensingLogCopy);

        return Result.successA(licensingLogCopy);
    }


    @GetMapping("/gotoIndex")
    public String godoIndx() {
        return "redirect:/index.html";
    }

    @GetMapping("/isAdmin")
    public Result isAdmin(@RequestParam("name") String name) {
        if (isAdmin.get()) {
            return Result.fail("已经有人坐庄了");
        }
        isAdmin.set(true);
        Map<String, String> param = new HashMap<>();
        param.put("log", "介把【" + name + "】过庄");
        licensingLog.push(param);
        flash();
        return Result.success(1);
    }

    @GetMapping("/isChecked")
    public Result<String> isChecked(@RequestParam(name = "model",required = false) String model) {
        if("undefined".equals(model)){
            return Result.fail("平民不能选模式");
        }
        if (isCheckedMap.get("zhuangState") != null) {
            if(!"ss".equals(model)){
                isCheckedMap.put("zhuangState",model);
            }
            return Result.success(isCheckedMap.get("zhuangState"));
        }
        if("ss".equals(model)){
            return Result.fail("平民不能选模式");
        }
        isCheckedMap.put("zhuangState",model);
        return Result.success(model);
    }

     // 重新开始， 只有庄家点击重新开始的时候会掉该接口
    @GetMapping("/reStart")
    public Result reStart() {
        isAdmin.set(false);
        licensingLog.clear();
        flopLog.clear();
        pubCard = "";
        pool.clear();
        zhuangListMap.clear();
        isCheckedMap.clear();
        return Result.success(1);
    }

    /**
     * 庄家控制发牌接口
     * @return
     */
    @RequestMapping("/zhuangControl")
    public Result<String> zhuangControl(String name,String model){
        String mySelfCard1 = "";
        String mySelfCard2 = "";
        Result<PrivateCard> selfCard = getSelfCard(name);
        if(selfCard.getData() != null || selfCard.getCode() == 200){
            mySelfCard1 = selfCard.getData().getSelfCard();
        }
        if("1".equals(model)){
            Result<PrivateCard> selfCard2 = getSelfCard(name);
            if(selfCard2.getData() != null || selfCard2.getCode() == 200){
                mySelfCard2 = selfCard2.getData().getSelfCard();
            }
        }
        if(name != null){
            if(!CollectionUtils.isEmpty(zhuangListMap)){
                for (int i = 0; i < zhuangListMap.size(); i++) {
                    if(name.equals(zhuangListMap.get(i).get("name"))){
                        zhuangListMap.remove(i);
                    }
                }
            }
            Map<String,String> map = new ConcurrentHashMap<>();
            map.put("name",name);
            map.put("mySelfCard1",mySelfCard1);
            if("1".equals(model)){
                map.put("mySelfCard2",mySelfCard2);
            }
            zhuangListMap.add(map);
        }
        return Result.success("1".equals(model) ? (mySelfCard1 + ":" + mySelfCard2) : mySelfCard1);
    }

    @GetMapping("/userRegister")
    public Result userRegister(String name){
        AtomicBoolean flag = new AtomicBoolean(false);
        if(!CollectionUtils.isEmpty(zhuangListMap)){
            zhuangListMap.forEach(map -> {
                if(name.equals(map.get("name"))){
                    flag.set(true);
                }
            });
        }
        if(flag.get()){
            return Result.fail("该用户重复登录！！");
        }
        Map<String,String> map = new ConcurrentHashMap<>();
        map.put("name",name);
        map.put("mySelfCard1","null");
        map.put("mySelfCard2","null");
        zhuangListMap.add(map);
        return Result.success(1);
    }

    @GetMapping("/getUsers")
    public Result<List> getUsers(){
        return Result.success(zhuangListMap);
    }

    @GetMapping("/userLogOut")
    public Result userLogOut(String name){
        if(!CollectionUtils.isEmpty(zhuangListMap)){
            for (int i = 0; i < zhuangListMap.size(); i++) {
                if(name.equals(zhuangListMap.get(i).get("name"))){
                    zhuangListMap.remove(i);
                }
            }
        }
        return Result.success(1);
    }

    @GetMapping("/getPrivCard")
    public Result<String> getPrivCard(String name,String model){
        String mySelfCard1 = "";
        String mySelfCard2 = "";
        if(!CollectionUtils.isEmpty(zhuangListMap)){
            for (int i = 0; i < zhuangListMap.size(); i++) {
                if(name.equals(zhuangListMap.get(i).get("name"))){
                    mySelfCard1 = zhuangListMap.get(i).get("mySelfCard1");
                    mySelfCard2 = zhuangListMap.get(i).get("mySelfCard2");
                    break;
                }
            }
        }
        return Result.success("1".equals(model) ? (mySelfCard1 + ":" + mySelfCard2) : mySelfCard1);
    }

    @GetMapping("/reSetCard")
    public Result reSetCard(String name,String model){
        if(!CollectionUtils.isEmpty(zhuangListMap)){
            for (int i = 0; i < zhuangListMap.size(); i++) {
                if(name.equals(zhuangListMap.get(i).get("name"))){
                    zhuangListMap.get(i).put("mySelfCard1","null");
                    if("1".equals(model)){
                        zhuangListMap.get(i).put("mySelfCard2","null");
                    }
                }
            }
        }
        return Result.success(1);
    }
}
